package com.sense.kukoo.biz_me.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_me.model.LoginModel
import com.sense.kukoo.common.event.LoginSuccessEvent
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.ResultBean
import com.sense.kukoo.common.repo.http.bean.LoginResultBean
import org.greenrobot.eventbus.EventBus

class LoginViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    val action_toFacebook:MutableLiveData<Boolean> = MutableLiveData()
    val action_toLine:MutableLiveData<Boolean> = MutableLiveData()
    val action_toGoogle:MutableLiveData<Boolean> = MutableLiveData()
    val action_toPhone:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun onClickFacebook(){
        action_toFacebook.postValue(true)
    }

    fun onClickLine(){
        action_toLine.postValue(true)
    }

    fun onClickGoogle(){
        action_toGoogle.postValue(true)
    }

    fun onClickPhone(){
        action_toPhone.postValue(true)
    }

    /**第三方登录*/
    fun login(avatarUrl: String, loginChannelKind: Int, nickName: String, userId: String){
        //
        toView(ACTION_SHOW_DIALOG)
        request {
            val result = Repo.http.thirdLogin(userId, loginChannelKind, avatarUrl, nickName)
            if(result.code == ResultBean.CODE_SUCCESS && result.success){
                val data: LoginResultBean = result.data!!
                //
                Repo.loginHistory.setLoginType(loginChannelKind)
                Repo.login.setLogin(data)
                //
                val userInfo = Repo.http.getUserInfo(0L)
                val user = userInfo.data!!
                Repo.user.setUser(user)
                //
                EventBus.getDefault().post(LoginSuccessEvent())
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }
}